package com.woniu108.product.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu108.carService.commos.dto.ServiceTypeDto;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import com.woniu108.product.form.UpdateServiceTypeForm;
import com.woniu108.product.model.ServiceType;
import com.woniu108.product.param.DeleteServiceTypeParam;
import com.woniu108.product.param.InsertServiceTypeParam;
import com.woniu108.product.param.UpdateServiceTypeParam;
import com.woniu108.product.service.ServiceTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务类型表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/serviceType")
@Api(tags = "服务种类接口")
public class ServiceTypeController {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private ServiceTypeService serviceTypeService;

    /**
     * 查所有服务种类
     * @return 所有服务种类
     */
    @GetMapping("/findAllServiceType.c")
    @ApiOperation("查所有服务种类")
    public Result<List<ServiceTypeDto>> findAllServiceType() {
        List<ServiceType> list = serviceTypeService.list();
        List<ServiceTypeDto> dtos = BeanUtil.copyToList(list, ServiceTypeDto.class);
        return Result.newListInstance(ServiceTypeDto.class).setData(dtos).setMsg("查所有服务种类成功");
    }
    /**
     * 新增服务种类
     * @param sTName 种类名
     * @return boolean值
     */
    @GetMapping("/insertServiceType.api")
    @ApiOperation("新增服务种类")
    @ApiImplicitParam(name = "sTName", value = "服务种类名")
    public Result<Boolean> insertServiceType(String sTName, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();

        InsertServiceTypeParam param =new InsertServiceTypeParam(sTName,adminId);
        Boolean bool = serviceTypeService.insertServiceType(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("新增服务种类成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("新增服务种类失败").setData(false).setCode(500);
    }
    /**
     * 修改服务种类名
     *@param form id,新种类名
     * @return boolean值
     */
    @PostMapping("/updateServiceType.api")
    @ApiOperation("修改服务种类名")
    public Result<Boolean> updateServiceType(@RequestBody UpdateServiceTypeForm form , @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();

        UpdateServiceTypeParam param =BeanUtil.toBean(form,UpdateServiceTypeParam.class);
        param.setAdminId(adminId);
        Boolean bool = serviceTypeService.updateServiceType(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("修改服务种类名成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("修改服务种类名失败").setData(false).setCode(500);
    }
    /**
     * 删除服务种类
     * @param sTId id
     * @return boolean值
     */
    @GetMapping("/deleteServiceType.api")
    @ApiOperation("删除服务种类")
    @ApiImplicitParam(name = "sTId", value = "服务种类编号")
    public Result<Boolean> deleteServiceType(Integer sTId, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();

        DeleteServiceTypeParam param =new DeleteServiceTypeParam(sTId,adminId);
        Boolean bool = serviceTypeService.deleteServiceType(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("删除服务种类成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("删除服务种类失败").setData(false).setCode(500);
    }
}

